home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 164 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  49 lines

  1. Path: cyberspace.com!loomis!icarus
  2. From: icarus@loomis (Tel Janin Aellinsar)
  3. Newsgroups: comp.lang.c
  4. Subject: Pointers to structures
  5. Date: 2 Jan 1996 21:59:00 GMT
  6. Organization: Chosen Suite #13, Shayol Ghul Resort and Health Spa
  7. Message-ID: <4cc9r4$m26@armitage.cyberspace.com>
  8. NNTP-Posting-Host: berkshire.net
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I'm having trouble using a pointer to a struct.  The idea is to have a 
  12. struct get filled by a function, which gets the address and everything 
  13. via function(struct type*).  Very straightforward.  However, if I try to 
  14. CHANGE anything in the struct, it just goes back when the function is 
  15. over.  So, let's pretend this imaginary structure is what I'm using:
  16.  
  17. struct st1 {
  18.   char *name;
  19.   int yadda;
  20. };
  21.  
  22. And the function is:
  23.  
  24. fn1(struct st1 *st)
  25. {
  26.   st = (struct st1*)malloc(sizeof(struct st1*));
  27.   st->name = (char*)malloc(16);
  28.   strcpy(st->name,"Test");
  29.   st->yadda = 100;
  30. }
  31.  
  32. At the last line of code in fn1, all the values are correct;  st->name is 
  33. "Test" and st->yadda is 100.  As soon as it exits, however, st->name 
  34. points to NULL and st->yadda is zero.  The allocation of the structure in 
  35. the first line of fn1 doesn't seem to matter;  both ways, I get NULL 
  36. pointers coming out of my ears.
  37.  
  38. Anybody who can help out there?  I'm not sure why it keeps bouncing 
  39. back...  there must be something I'm missing, but I don't see why it 
  40. shouldn't work the way it does.
  41.  
  42. E-mail replies, please...  thanks!
  43.  
  44. --
  45. ________________________________________________________________________
  46. Tel Janin Aellinsar                       http://www.crocker.com/~icarus
  47. McCoy Enterprises Corporation                  info@ICARUS.BERKSHIRE.NET
  48. Berserker Dragon, Knights of the Cosmos             icarus@BERKSHIRE.NET
  49.